from django.db import models
import uuid  # 用于生成 UUID


class Game(models.Model):
    DIFFICULTY_CHOICES = (
        ('easy', '简单'),
        ('medium', '中等'),
        ('hard', '困难'),
    )

    # 使用 UUIDField 自动生成主键，更符合数据库设计
    game_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    maze_data = models.JSONField()  # Django 3.1+ 支持 JSONField
    maze_size = models.IntegerField()
    difficulty = models.CharField(max_length=10, choices=DIFFICULTY_CHOICES)
    start_time = models.DateTimeField(auto_now_add=True)
    end_time = models.DateTimeField(null=True, blank=True)
    game_time = models.IntegerField(default=0)  # 游戏进行时间(秒)
    best_time = models.IntegerField(default=0)  # 最佳时间(秒)
    is_completed = models.BooleanField(default=False)

    class Meta:
        db_table = 'games'

    def __str__(self):
        return f"Game {self.game_id}"


class Player(models.Model):
    # 使用 UUIDField 自动生成主键
    player_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    # 建立外键关联，更符合数据库关系设计
    game = models.ForeignKey(Game, on_delete=models.CASCADE, related_name='players')
    player_number = models.PositiveSmallIntegerField()  # 确保为正整数
    x_position = models.IntegerField()
    y_position = models.IntegerField()
    color = models.CharField(max_length=7)  # 支持如 #FF0000 格式
    is_completed = models.BooleanField(default=False)
    last_active = models.DateTimeField(auto_now=True)  # 每次更新时自动刷新

    class Meta:
        db_table = 'players'
        # 为单个字段添加索引
        indexes = [
            models.Index(fields=['game']),
            models.Index(fields=['player_number']),
        ]

    def __str__(self):
        return f"Player {self.player_number} in Game {self.game.game_id}"

class Player_position_id1(models.Model):
    player_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    # 建立外键关联，更符合数据库关系设计
    game = models.ForeignKey(Game, on_delete=models.CASCADE, related_name='player_position_id1')
    player_number = models.PositiveSmallIntegerField()  # 确保为正整数
    x_position = models.IntegerField()
    y_position = models.IntegerField()
    color = models.CharField(max_length=7)  # 支持如 #FF0000 格式
    is_completed = models.BooleanField(default=False)
    last_active = models.DateTimeField(auto_now=True)  # 每次更新时自动刷新

    class Meta:
        db_table = 'players_position_id1'


class Player_position_id2(models.Model):
    player_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    # 建立外键关联，更符合数据库关系设计
    game = models.ForeignKey(Game, on_delete=models.CASCADE, related_name='player_position_id2')
    player_number = models.PositiveSmallIntegerField()  # 确保为正整数
    x_position = models.IntegerField()
    y_position = models.IntegerField()
    color = models.CharField(max_length=7)  # 支持如 #FF0000 格式
    is_completed = models.BooleanField(default=False)
    last_active = models.DateTimeField(auto_now=True)  # 每次更新时自动刷新

    class Meta:
        db_table = 'players_position_id2'
        # 为单个字段添加索引

class Player_position_id3(models.Model):
    player_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    # 建立外键关联，更符合数据库关系设计
    game = models.ForeignKey(Game, on_delete=models.CASCADE, related_name='player_position_id3')
    player_number = models.PositiveSmallIntegerField()  # 确保为正整数
    x_position = models.IntegerField()
    y_position = models.IntegerField()
    color = models.CharField(max_length=7)  # 支持如 #FF0000 格式
    is_completed = models.BooleanField(default=False)
    last_active = models.DateTimeField(auto_now=True)  # 每次更新时自动刷新

    class Meta:
        db_table = 'players_position_id3'
        # 为单个字段添加索引

class Player_position_id4(models.Model):
    player_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    # 建立外键关联，更符合数据库关系设计
    game = models.ForeignKey(Game, on_delete=models.CASCADE, related_name='player_position_id4')
    player_number = models.PositiveSmallIntegerField()  # 确保为正整数
    x_position = models.IntegerField()
    y_position = models.IntegerField()
    color = models.CharField(max_length=7)  # 支持如 #FF0000 格式
    is_completed = models.BooleanField(default=False)
    last_active = models.DateTimeField(auto_now=True)  # 每次更新时自动刷新

    class Meta:
        db_table = 'players_position_id4'
        # 为单个字段添加索引

class GameState(models.Model):
    state_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    # 建立外键关联
    game = models.ForeignKey(Game, on_delete=models.CASCADE, related_name='states')
    current_player = models.PositiveSmallIntegerField()  # 1-4
    is_solving = models.BooleanField(default=False)

    class Meta:
        db_table = 'game_state'
        # 确保每个游戏同一时间只有一个状态
        unique_together = [('game',)]

    def __str__(self):
        return f"State of Game {self.game.game_id}"


class Score(models.Model):
    score_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    # 建立外键关联
    game = models.ForeignKey(Game, on_delete=models.CASCADE, related_name='scores')
    player = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='scores')
    score = models.IntegerField(default=0)
    completion_time = models.IntegerField(default=0)  # 完成时间(秒)

    class Meta:
        db_table = 'scores'
        # 确保每个玩家在每个游戏中只有一条分数记录
        unique_together = [('game', 'player')]

    def __str__(self):
        return f"Score of Player {self.player.player_number} in Game {self.game.game_id}"